package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.client.ItemClient;
import com.hmall.common.client.UserClient;
import com.hmall.common.dto.AddressDTO;
import com.hmall.common.dto.ItemDTO;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.RequestParams;
import com.hmall.order.service.IOrderService;
import com.hmall.order.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;

    @Transactional
    @Override
    public Long createOrder(RequestParams params) {
        //根据itemId查询商品信息
        ItemDTO itemDTO = itemClient.selectItemById(params.getItemId());
        //基于商品价格、购买数量计算商品总价:totalFee
        Long totalFee = params.getNum()*itemDTO.getPrice();
        //封装Order对象，初始status为未支付
        Order order = new Order().builder()
                .totalFee(totalFee)
                .paymentType(params.getPaymentType())
                .userId(BaseContext.getCurrentId())
                .status(1)
                .createTime(new Date())
                .updateTime(new Date())
                .build();
        System.out.println(order.getId());
        //将Order写入数据库tb_order表中
        orderMapper.insert(order);
        //将商品信息、orderId信息封装为OrderDetail对象，写入tb_order_detail表
        OrderDetail orderDetail = new OrderDetail().builder()
                .name(itemDTO.getName())
                .spec(itemDTO.getSpec())
                .price(itemDTO.getPrice())
                .num(params.getNum())
                .itemId(itemDTO.getId())
                .image(itemDTO.getImage())
                .orderId(order.getId())
                .build();
        orderDetailMapper.insert(orderDetail);
        //将user-service的根据id查询地址接口封装为FeignClient
        //根据addressId查询user-service服务，获取地址信息
        AddressDTO addressDTO = userClient.findAddressById(params.getAddressId());
        //将地址封装为OrderLogistics，写入tb_order_logistics表
        OrderLogistics orderLogistics = new OrderLogistics().builder()
                .orderId(order.getId())
                .contact(addressDTO.getContact())
                .mobile(addressDTO.getMobile())
                .province(addressDTO.getProvince())
                .city(addressDTO.getCity())
                .town(addressDTO.getTown())
                .street(addressDTO.getStreet())
                .createTime(new Date())
                .updateTime(new Date())
                .build();
        orderLogisticsMapper.insert(orderLogistics);
        //扣减库存
        itemClient.substractStock(params.getItemId(), params.getNum());
        return order.getId();
    }
}
